package z.t.b.db.data;


import z.t.b.db.meta.Column;
import z.t.b.db.utils.StrUtils;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 行数据
 */
public class RowData extends HashMap<String, Object> implements KeyValue{

    private TableData owner;

    public TableData getOwner() {
        return owner;
    }

    public void setOwner(TableData owner) {
        this.owner = owner;
    }

    @Override
    public Object getValue(String key, Object def) {
        return MapUtils.getObject(this, key, def);
    }

    @Override
    public void setValue(String key, Object val) {
        MapUtils.putObject(this, key, val);
    }

    @Override
    public Number getNumber(String key, Number def) {
        return MapUtils.getNumber(this, key, def);
    }

    @Override
    public void putNumber(String key, Number val) {
        MapUtils.putObject(this, key, val);
    }

    @Override
    public String getString(String key, String def) {
        return MapUtils.getString(this, key, def);
    }

    @Override
    public void putString(String key, String val) {
        MapUtils.putObject(this, key, val);
    }

    @Override
    public void putObject(String key, Object val) {
        this.setValue(key, val);
    }

    @Override
    public int getInt(String key, int def){
        Object o = this.getValue(key, null);
        if (o == null) {
            return def;
        } else if (o instanceof String) {
            return Integer.parseInt((String)o);
        } else {
            return o instanceof Number ? ((Number)o).intValue() : def;
        }
    }

    /**
     *
     * @param col
     * @param def
     * @return
     */
    public String getValueToSqlString(String col, String def){
        Column column = getOwner().getColumnById(col);
        if(column == null){
            return "null";
        }
        String type = column.getType();
        if(type.indexOf("CHAR") !=-1 || type.indexOf("VARCHAR") != -1 || type.indexOf("TEXT") != -1){
            String strValue = getString(col, def);
            if(strValue == null){
                return "null";
            }
            return StrUtils.getDbString(strValue);
        }else if(type.indexOf("DATE")!=-1 || type.indexOf("TIME")!=-1){
            Object value = getValue(col, def);
            if(value == null){
                return "null";
            }else{
                return StrUtils.getDbString(value.toString());
            }
        }else{
            Object value = getValue(col, def);
            if(value == null){
                return "null";
            }else{
                return value.toString();
            }
        }
    }

    /**
     *
     * @return
     */
    public String toInsertSql(){
        TableData tableData = this.getOwner();
        List<String> colList = tableData.getColumnList();
        StringBuilder sql = new StringBuilder();
        sql.append("INSERT INTO ").append(tableData.getTableName());
        sql.append("(").append(colList.stream().collect(Collectors.joining(","))).append(")");
        sql.append("VALUES(").append(colList.stream().map(col->{
            return getValueToSqlString(col, null);
        }).collect(Collectors.joining(","))).append(")");
        return sql.toString();
    }
}
